package gov.child.service.impl;

import gov.child.dao.DepartmentDao;
import gov.child.entities.po.EDepartment;
import gov.child.entities.vo.BeanTransformer;
import gov.child.entities.vo.Department;
import gov.child.entities.vo.Page;
import gov.child.entities.vo.PageUtil;
import gov.child.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author fzh
 * @since 2017/9/14
 */
@Service("departmentService")
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentDao departmentDao;

    @Override
    public List findAll() {
        return BeanTransformer.parseFrom(Department.class, departmentDao.findAll());
    }

    @Override
    public List findNameAndId() {
        return BeanTransformer.parseFrom(Department.class, departmentDao.findNameAndId());
    }

    @Override
    public boolean save(Department department) {
        departmentDao.save(Department.parseTo(department));
        return true;
    }

    @Override
    public Department findById(Integer id) {
        EDepartment department = departmentDao.findById(id);
        if (Objects.isNull(department)) return Department.EMPTY_DEPARTMENT;
        return Department.parseFrom(department);
    }

    @Override
    public List findByDepartmentParentId(Integer id) {
        List l = departmentDao.findByDepartmentParentId(id);
        return BeanTransformer.parseFrom(Department.class, l);
    }

    @Override
    public List findParentDepartment() {
        List l = departmentDao.findParentDepartment();
        return BeanTransformer.parseFrom(Department.class, l);
    }

    @Override
    public Page getPage(int pageSize) {
        long total = departmentDao.total();
        return PageUtil.createPage(pageSize, total, 1);
    }
}
